﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Vowei.Data;
using Vowei.Data.Helpers;
using Vowei.Web.Helpers;
using Vowei.Converters.Helpers;
using Vowei.Data.Models;
using Vowei.Core.Models;
using Vowei.Web.Models;
using Vowei.Converters.Excel;
using System.IO;

namespace Vowei.Web.Controllers
{
    public class StatisticsController : G18nController
    {
        private IVoweiContext _context;
        public StatisticsController(IVoweiContext context)
        {
            _context = context;
        }

        public ActionResult RequirementWorks(Guid pid)
        {
            var project = _context.Projects.Find(pid);
            if (project == null)
                throw new ArgumentException(string.Format(T("找不到ID为 [{0}] 的项目!"), pid));

            var works = project.TopRequirementWorks(_context);
            return View(works).AddJsonSupport();
        }

        public ActionResult EarnedValueOrig(Guid pid)
        {
            var project = _context.Projects.Find(pid);
            if (project == null)
                throw new ArgumentException(string.Format(T("找不到ID为 [{0}] 的项目!"), pid));

            var works = project.EarnedValuesObsolete(_context);
            return View(works).AddJsonSupport();
        }

        public ActionResult EarnedValue(Guid pid)
        {
            var project = _context.Projects.Include("ResourceAssignments").Query.Single(p => p.ID == pid);
            if (project == null)
                throw new ArgumentException(string.Format(T("找不到ID为 [{0}] 的项目!"), pid));

            var works = project.EarnedValues(_context);
            ViewData[WebConstants.ViewData.Project] = project;
            return View(works);
        }

        public ActionResult EvDetails(Guid id, long date)
        {
            var project = _context.Projects.Find(id);
            if (project == null)
                throw new ArgumentException(string.Format(T("找不到ID为 [{0}] 的项目!"), id));

            var time = new DateTime(date);
            var tickets = project.PlanDetails(time, _context);
            ViewData[WebConstants.ViewData.Project] = project;
            return View(tickets);
        }

        public ActionResult ListTopReq(Guid pid)
        {
            var project = _context.Projects.Find(pid);
            if (project == null)
                throw new ArgumentException(string.Format(T("找不到ID为 [{0}] 的项目!"), pid));

            ViewData[WebConstants.ViewData.Project] = pid;
            return View(project.TopRequirementList(_context));
        }

        private string ProjectScheduleExcelTemplatePath;

        public ActionResult ExportProjectTasks(Guid pid)
        {
            var project = _context.Projects.Find(pid);
            if (project == null)
                throw new ArgumentException(string.Format(T("找不到ID为 [{0}] 的项目!"), pid));

            if (string.IsNullOrEmpty(ProjectScheduleExcelTemplatePath))
                ProjectScheduleExcelTemplatePath = Server.MapPath("/Content/tmpl/Project schedule template.xls");

            Response.ContentType = WebConstants.ApplicationExcel;
            Response.AddHeader("Content-Disposition", string.Format("attachment;Filename={0}.xls", project.Title));
            
            project.ConvertToExcel(_context, Response.OutputStream, ProjectScheduleExcelTemplatePath);
            return WebConstants.HttpSuccess;
        }

        public ActionResult Connect(Guid id, string type)
        {
            var project = _context.Projects.Find(id);
            if (project == null)
                throw new ArgumentException(string.Format(T("找不到ID为 [{0}] 的项目!"), id));

            Response.ContentType = WebConstants.ApplicationExcel2007;
            Response.AddHeader("Content-Disposition", string.Format("attachment;Filename={0}_{1}.xlsx", project.Title.Replace(' ', '_'), type));
            var converter = CreateConvert(type);

            using (var stream = new MemoryStream())
            {
                converter.ConvertTo(Request.UrlReferrer.AbsoluteUri, stream);
                stream.Seek(0, SeekOrigin.Begin);
                stream.CopyTo(Response.OutputStream);
            }

            return WebConstants.HttpSuccess;
        }

        private DataConnectionConverter CreateConvert(string type)
        {
            switch (type)
            {
                case WebConstants.Statistics.RequirementStatistics:
                    return new RequirementStatisticsExcelGenerator(Translator);

                case WebConstants.Statistics.EarnedValue:
                    return new EarnedValueStatisticsExcelGenerator(Translator);

                default:
                    throw new NotSupportedException(string.Format(T("尚不支持统计类型：{0}。"), type));
            }
        }
    }
}
